package org.qf.web;

import org.qf.service.SmbmsUserService;
import org.qf.service.impl.SmbmsUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author young
 * @create 2021-01-15-13:57
 * @description
 */
@WebServlet(value = "/getPasswordServlet")
public class PasswordServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SmbmsUserService smbmsUserService=new SmbmsUserServiceImpl();
        String oldPassword=req.getParameter("oldPassword");
        long oldPasswords = Long.parseLong(oldPassword);
        String newPassword=req.getParameter("newPassword");
        long newPasswords=Long.parseLong(newPassword);
        String reNewPassword=req.getParameter("reNewPassword");
        long renewPasswords=Long.parseLong(reNewPassword);
        if(newPasswords!=renewPasswords)
        {
            req.getRequestDispatcher("password.jsp").forward(req,resp);
        }

        try {
            int i =smbmsUserService.upadatePw(oldPasswords,renewPasswords);
            if(i>0)
            {
                req.getRequestDispatcher("userServlet").forward(req,resp);
            }else
            {
                req.getRequestDispatcher("password.jsp").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
